package com.redoor.app.stake.basicdata.mq.consumerService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.redoor.app.stake.basicdata.model.BaseMqOperationLog;
import com.redoor.app.stake.basicdata.model.BaseStake;;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStakeModifyBean;
import com.redoor.app.stake.basicdata.mq.sendService.SendMQService;
import com.redoor.app.stake.basicdata.service.BaseStakeService;
import com.redoor.app.stake.basicdata.service.LogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by zhangYiHang on 2018/4/9.
 */
public class BaseStakeMessageListener implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(BaseStakeMessageListener.class) ;

    @Autowired
    private BaseStakeService baseStakeService ;

    @Autowired
    LogService logService;


    /**
     * 监听处理程序
     *
     * @param message
     * @param context
     * @return
     */
    @Override
    public Action consume(Message message, ConsumeContext context) {
        String json = new String(message.getBody()) ;
        String tag = message.getTag() ;
        logger.info("work sheet message listener get mq body = " + json) ;
        if(StringUtils.isEmpty(json)){
            logger.info("topic taskTopic95598 message listener get empty mq tag=" + tag) ;
        } else {
            /*//工单处理
            if("FINISH".equals(tag) || "BACK".equals(tag)){
                this.updateWorkSheet(json) ;
            }
            else*/
            //停复运流程
            if("CHANGE".equals(tag)){
                BaseMqOperationLog baseMqOperationLog = new BaseMqOperationLog();
                baseMqOperationLog.setUuid(UUIDTool.getUUID());
                baseMqOperationLog.setMessagetopic(message.getTopic());
                baseMqOperationLog.setMessagebody(json);
                baseMqOperationLog.setMessagetag(tag);
                baseMqOperationLog.setType(2);

                this.updateStakeStatus(json,baseMqOperationLog) ;
            }
        }
        return Action.CommitMessage ;
    }

    /**
     * 处理停复运流程
     *
     * @param json
     */
    private void updateStakeStatus(String json,BaseMqOperationLog baseMqOperationLog){
        try{
            JSONObject object = JSON.parseObject(json) ;
            //接收传入的桩编码
            String stakeNo = object.getString("stakeNo");
            //根据桩编码查询桩信息
            BaseStake baseStake = new BaseStake();
            baseStake.setStakeno(stakeNo);
            BaseStake stake = this.baseStakeService.selectOne(baseStake);
            //如果桩不存在返回
            if(stake == null || stake.getStatus() != 1000){
                logger.info("ywjx update stake status failed stake not found " + stakeNo) ;
                return ;
            }
            String status = object.getString("currStatus") ;
            int stakeStatus = "停运".equals(status) ? 10 : ("运行".equals(status) ? 3 : 0) ;
            if(stakeStatus == 0){
                logger.info("ywjx update stake status failed status=" + status) ;
                return ;
            }
            stake.setStakestatus(stakeStatus);
            BaseStakeModifyBean baseStakeModifyBean = new BaseStakeModifyBean();
            baseStakeModifyBean.setBaseStake(stake);

            logService.addLogToRedis(baseMqOperationLog, LogEnum.MQ_LOG);
            this.baseStakeService.updateStakeModify(baseStakeModifyBean, BussinessTypeEnum.TRACE_TYPE.MQ,baseMqOperationLog.getUuid());

            logger.info("ywjx update stake status send mq ok") ;
        } catch(Exception e){
            logger.info("ywjx update stake status throws exception ") ;
            e.printStackTrace() ;
        }
    }
}
